package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.PlanDetail;
import com.isunland.managesystem.entity.PlanDetailOriginal;
import com.isunland.managesystem.entity.WorkprocessFinish.FinishDtail;
import com.isunland.managesystem.utils.DateUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import org.json.JSONException;

public class WorkProcessFinishDetailFragment extends Fragment {

  public static final String EXTRA_VALUE = "com.isunland.managesystem.ui.EXTRA_VALUE";
  public static final String EXTRA_FROM =
      "com.isunland.managesystem.ui.WorkProcessFinishDetailFragment.EXTRA_FROM";
  public static final int VALUE_TASK_COLLECT = 1;
  private TextView mtv_appointer_workProcessFinished;// 委派人
  private String mAppointer;
  private TextView mtv_register_workProcessFinished;// 登记人
  private String mRegister;
  private TextView mtv_corporator_workProcessFinished;// 配合人
  private String mCorporator;
  private TextView mtv_executor_workProcessFinished;// 执行人
  private String mExecutor;
  private TextView mtv_applicator_workProcessFinished;// 申报人
  private String mApplicator;
  private TextView mtv_latestFinishTime_workProcessFinished;// 最新阶段完成时间
  private String mLatestFinishTime;
  private TextView mtv_requestFinishTime_workProcessFinished;// 要求完成时间
  private String mRequestFinishTime;
  private TextView mtv_registerTime_workProcessFinished;// 登记日期
  private String mRegisterTime;
  private TextView mtv_sumTime_workProcessFinished;// 总工时
  private String mSumTime;
  private TextView mtv_planType_workProcessFinished;// 计划性质
  private String mPlanType;
  private TextView mtv_statusPlan_workProcessFinished;// 计划状态
  private String mStatusPlan;
  private TextView mtv_finishCondition_workProcessFinished;// 完成情况
  private String mFinishCondition;
  private TextView mtv_evaluteContent_workProcessFinished;// 评价内容
  private String mEvaluteContent;
  private TextView mtv_planContent_workProcessFinished;// 计划任务内容
  private String mPlanContet;
  private TextView mTvIfEnd;
  private TextView mTvRate;
  private TextView mTvPlanKindName;
  private FinishDtail mDetail;
  private BaseVolleyActivity mActivity;

  //	public static Fragment newInstance(FinishDtail detail) {
  //		Bundle bundle = new Bundle();
  //		bundle.putSerializable(EXTRA_VALUE, detail);
  //		WorkProcessFinishDetailFragment fragment = new WorkProcessFinishDetailFragment();
  //		fragment.setArguments(bundle);
  //		return fragment;
  //	}
  public static Fragment newInstance(FinishDtail detail, int from) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VALUE, detail);
    bundle.putInt(EXTRA_FROM, from);
    WorkProcessFinishDetailFragment fragment = new WorkProcessFinishDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);

    mDetail = (FinishDtail) getArguments().getSerializable(EXTRA_VALUE);
    int mFrom = getArguments().getInt(EXTRA_FROM);
    mActivity = ((BaseVolleyActivity) getActivity());
    ActionBar actionBar = mActivity.getSupportActionBar();
    if (actionBar != null) {
      actionBar.setDisplayHomeAsUpEnabled(true);
      if (mFrom == VALUE_TASK_COLLECT) {
        actionBar.setTitle("任务详情");
      } else {
        actionBar.setTitle(R.string.workprocessFinish_detail);
      }
    }

    if (mDetail != null) {
      mAppointer = mDetail.getTaskManName();
      mRegister = mDetail.getRegStaffName();
      mCorporator = mDetail.getCoopManNames();
      mExecutor = mDetail.getExcManName();
      mApplicator = mDetail.getPlanAppdeptName();
      mLatestFinishTime = mDetail.getPlanStagecompleteTime();
      mRequestFinishTime = mDetail.getPlanEsteTime();
      mRegisterTime = mDetail.getRegDate();
      mSumTime = mDetail.getWorkTime();
      mPlanType = mDetail.getPlanKindType();
      mStatusPlan = mDetail.getPlanStatus();
      mFinishCondition = mDetail.getPlanExcDesc();
      mEvaluteContent = mDetail.getResultDescEvalu();
      mPlanContet = mDetail.getPlanContentDesc();
    }
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_workprocess_finishdetail, container, false);
    mtv_appointer_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_appointer_workProcessFinished);
    mtv_register_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_register_workProcessFinished);
    mtv_corporator_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_corporator_workProcessFinished);
    mtv_executor_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_executor_workProcessFinished);
    mtv_applicator_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_applicator_workProcessFinished);
    mtv_latestFinishTime_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_latestFinishTime_workProcessFinished);
    mtv_requestFinishTime_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_requestFinishTime_workProcessFinished);
    mtv_registerTime_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_registerTime_workProcessFinished);
    mtv_sumTime_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_sumTime_workProcessFinished);
    mtv_planType_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_planType_workProcessFinished);
    mtv_statusPlan_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_statusPlan_workProcessFinished);
    mtv_finishCondition_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_finishCondition_workProcessFinished);
    mtv_evaluteContent_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_evaluteContent_workProcessFinished);
    mtv_planContent_workProcessFinished =
        (TextView) view.findViewById(R.id.tv_planContent_workProcessFinished);
    mTvIfEnd = (TextView) view.findViewById(R.id.tv_ifEnd_workProcessFinished);
    mTvRate = (TextView) view.findViewById(R.id.tv_rate_workProcessFinished);
    mTvPlanKindName = (TextView) view.findViewById(R.id.tv_class_workProcessFinished);

    initData();

    return view;
  }

  private void initData() {

    if (!TextUtils.isEmpty(mAppointer)) {
      mtv_appointer_workProcessFinished.setText(mAppointer);
    }
    if (!TextUtils.isEmpty(mRegister)) {
      mtv_register_workProcessFinished.setText(mRegister);
    }
    if (!TextUtils.isEmpty(mCorporator)) {
      mtv_corporator_workProcessFinished.setText(mCorporator);
    }
    if (!TextUtils.isEmpty(mExecutor)) {
      mtv_executor_workProcessFinished.setText(mExecutor);
    }
    if (!TextUtils.isEmpty(mApplicator)) {
      mtv_applicator_workProcessFinished.setText(mApplicator);
    }
    if (!TextUtils.isEmpty(mLatestFinishTime)) {
      mtv_latestFinishTime_workProcessFinished.setText(mLatestFinishTime);
    }

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    if (!TextUtils.isEmpty(mRequestFinishTime)) {
      Date finishDate = DateUtil.stringToDate(mRequestFinishTime);
      String finishTime = format.format(finishDate);
      mtv_requestFinishTime_workProcessFinished.setText(finishTime);
    }
    if (!TextUtils.isEmpty(mRegisterTime)) {
      Date registDate = DateUtil.stringToDate(mRegisterTime);
      String registTime = format.format(registDate);
      mtv_registerTime_workProcessFinished.setText(registTime);
    }
    if (!TextUtils.isEmpty(mSumTime)) {
      mtv_sumTime_workProcessFinished.setText(mSumTime);
    }
    if (!TextUtils.isEmpty(mPlanType)) {
      mtv_planType_workProcessFinished.setText(mPlanType);
    }
    if (!TextUtils.isEmpty(mStatusPlan)) {
      mtv_statusPlan_workProcessFinished.setText(mStatusPlan);
    }
    if (!TextUtils.isEmpty(mFinishCondition)) {
      mtv_finishCondition_workProcessFinished.setText(mFinishCondition);
    }
    if (!TextUtils.isEmpty(mEvaluteContent)) {
      mtv_evaluteContent_workProcessFinished.setText(mEvaluteContent);
    }
    if (!TextUtils.isEmpty(mPlanContet)) {
      mtv_planContent_workProcessFinished.setText(mPlanContet);
    }
  }

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (mDetail != null && mDetail.getId() != null) {
      volleyPostInit(mDetail.getId());
    }
  }

  private void volleyPostInit(String id) {
    if (TextUtils.isEmpty(id)) return;
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_GET_PLAN_DETAIL);
    HashMap<String, String> params = new HashMap<>();
    params.put("type", "mobile");
    params.put("id", id);
    //MyUtils.showDialogLoading(getActivity(),R.string.loading);
    mActivity.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        PlanDetailOriginal original = new Gson().fromJson(arg0, PlanDetailOriginal.class);
        if (original.getResult() == Base.SUCCESS) {
          PlanDetail data = original.getData();
          if (data.getIfEnd() != null && data.getIfEnd().equals("T")) {

            mTvIfEnd.setText(R.string.isTrue);
          }
          if (data.getIfEnd() != null && data.getIfEnd().equals("F")) {

            mTvIfEnd.setText(R.string.isFalse);
          }
          mTvRate.setText(data.getRateCurrent());
          mTvPlanKindName.setText(data.getPlanKindName());
        }
      }
    });
  }

  /**
   * 响应ActionBar中的按钮
   */
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        // if (NavUtils.getParentActivityName(getActivity()) != null) {
        // NavUtils.navigateUpFromSameTask(getActivity());
        // }
        getActivity().finish();
        break;
      default:
        return super.onOptionsItemSelected(item);
    }
    return super.onOptionsItemSelected(item);
  }
}
